package com.fct.controller;

import com.fct.modal.User;
import com.fct.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

/**
 * 首页相关的action  不同类型的参数传递
 * Created by Xstarfct on 2017/1/11.
 */
@Controller
public class IndexAction {

    @Autowired
    private UserService userService;

    @ResponseBody
    @RequestMapping("getAllUsers.do")
    public List<User> getAllUsers() {
        System.out.println("11111");
        System.out.println("222222");
        return userService.getAllUsers();
    }

    /**
     * 默认传参方式
     *
     * @param model 默认封装的数据
     * @return 一个jsp页面
     */
    @RequestMapping("getUser.do")
    public String getUser(Model model) {
        User user = new User();
        user.setId(111L);
        user.setPassword("123456");
        user.setUserName("fct");
        model.addAttribute("user", user);
        return "user";
    }

    /**
     * 带参数的路由配置
     *
     * @param name name
     * @return user  json对象
     */
    @ResponseBody
    @RequestMapping(value = "addUser/{name}.do", method = RequestMethod.GET)
    public User addUser(@PathVariable String name) {
        User user = new User();
        user.setId(111L);
        user.setPassword("123456");
        user.setUserName(name);
        return user;
    }

    /**
     * 使用request/response取得参数
     * @param request
     * @param response
     * @return
     */
    @RequestMapping(value="/index9.do")
    public String index9(HttpServletRequest request, HttpServletResponse response) {
        String message = request.getParameter("message");
        request.setAttribute("message", message);
        return "user";
    }

    /**
     * 使用@RequestParam方式取得参数
     * @param message
     * @return
     */
    @RequestMapping(value="/index10.do")
    public ModelAndView index10(@RequestParam("message") String message) {
        ModelAndView view = new ModelAndView("user");
        view.addObject("message", message);
        return view;
    }

    /**
     * 传入PrintWriteer，也可以传入@CookieValue
     *
     * @param out
     * @param message
     * @param model
     * @return
     */
    @RequestMapping(value = "/index12.do")
    public String index12(PrintWriter out, String message, Model model) {
        out.println(message);
        model.addAttribute("message", message);
        return "user";
    }

    /**
     * 日期参数
     *
     * @param day
     * @return
     */
    @RequestMapping(value = "/index13.do")
    public ModelAndView index13(@RequestParam("day") @DateTimeFormat(pattern = "yyyy-MM-dd") Date day) {
        ModelAndView view = new ModelAndView("user");
        view.addObject("message", day.toString());
        return view;
    }

    /**
     * 默认action
     *
     * @param message
     * @return
     */
    @RequestMapping
    public ModelAndView index14(String message) {
        ModelAndView view = new ModelAndView("index");
        view.addObject("message", message);
        return view;
    }


    /**
     * 限制访问的地址，必需有参数p=1才能触发
     *
     * @param message
     * @param model
     * @return
     */
    @RequestMapping(value = "/index15/{message}.do", params = "p=1", method = RequestMethod.GET)
    public String index15(@PathVariable("message") String message, Model model) {
        model.addAttribute("message", message);
        return "user";
    }

}
